<?php
class Group {
	var $id = 0;
	var $linkid = 0;
	var $name = '';
	var $desc = '';
	var $type = 'normal';
	var $stage = 'normal';
	var $privacy = 'open';
	var $users = 1;

	function store() {
		global $db, $current_user;
		
		$group_id = $this->id;
		$group_link_id = $this->linkid;
		$group_name = $this->name;
		$group_desc = $this->desc;
		$group_type = $this->type;
		$group_stage = $this->stage;
		$group_privacy = $this->privacy;
		$group_users = $this->users;
	
		if($this->id===0) {
			$sql = "INSERT INTO " . table_group . " (group_link_id, group_name, group_desc, group_type, group_stage, group_privacy, group_users) VALUES ($group_link_id,'$group_name', '$group_desc', '$group_type', '$group_stage', '$group_privacy', $group_users)";
			if($this->debug == true){echo '<hr>store_basic:Insert:' . $sql . '<hr>';}
			$db->query($sql);
			$this->id = $db->insert_id;
		} else {
		// update
			$sql = "UPDATE " . table_group . " set `group_link_id`=$group_link_id, `group_name`='$group_name', `group_desc`='$group_desc', `group_type`='$group_type', `group_stage`='$group_stage', `group_privacy`='$group_privacy', `group_users`=$group_users WHERE group_id=$this->id";
			if($this->debug == true){echo '<hr>store_basic:Update:' . $sql . '<hr>';}
			$db->query($sql);
		}
	}

	function read($usecache = TRUE) {
		global $db, $current_user, $cached_group;
		$id = $this->id;
		
		if ($id == 0) {
			//get $id
			if ($this->linkid > 0) {
				$this->id = $db->get_var("SELECT group_id FROM " . table_group . " WHERE group_link_id=" . $this->linkid);
				$id = $this->id;
			}
			else {
				return false;
			}
		}

		// check to see if the link is cached
		// if it is, use it
		// if not, get from mysql and save to cache
		
		if (isset($cached_group[$id]) && $usecache == TRUE) {
			$group = $cached_group[$id];
		} else {
			$group = $db->get_row("SELECT " . table_group . ".* FROM " . table_group . " WHERE group_id = $id");
			$cached_group[$id] = $group;
		}
    
		if($group) {
			$group_link_id = $group->group_link_id;
			$group_name = $group->group_name;
			$group_desc = $group->group_desc;
			$group_type = $group->group_type;
			$group_stage = $group->group_stage;
			$group_privacy = $group->group_privacy;
			$group_users = $group->group_users;
			$this->linkid = $group_link_id;
			$this->name = $group_name;
			$this->desc = $group_desc;
			$this->type = $group_type;
			$this->stage = $group_stage;
			$this->privacy = $group_privacy;
			$this->users = $group_users;
			return true;
		}
		return false;
	}
	
	function setowner($ownerid) {
		// check if owner already exist
		global $db;
		$oldownerid = $db->get_var("SELECT gu_user_id FROM " . table_group_user . " WHERE gu_user_type='owner' AND gu_user_state='approved' AND gu_group_id=" . $this->id); 
		if ($oldownerid) {
			if ($oldownerid != $ownerid) {
				$sql = "UPDATE " . table_group_user . " SET gu_user_type='normal' WHERE gu_group_id=" . $this->id . " AND gr_user_id=" . $oldownerid;
				$db->query($sql);	
				return true;
			}
			else {
				return true;
			}
		}
		
		$alreadyuser = $db->get_var("SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_group_id=" . $this->id . " AND gu_user_id=" . $ownerid);
		if ($alreadyuser > 0) {
			$sql = "UPDATE " . table_group_user . " SET gu_user_type='owner', gu_user_state='approved' WHERE gu_user_id=" . $ownerid . " AND gu_group_id=" . $this->id;
			$db->query($sql);
			return true;	
		}	
		else {
			return $this->add_user($ownerid, 'owner', 'approved');
		}
	}

	function add_user($userid, $user_type, $user_state) {
		global $db;
		if (!($userid > 0)) {
			return false;
		}
		if ($this->id == 0) {
			$this->read();
		}
		$alreadyuser = $db->get_var("SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_group_id=" . $this->id . " AND gu_user_id=" . $userid);
		if ($alreadyuser > 0) {
			$sql = "UPDATE " . table_group_user . " SET gu_user_type='$user_type', gu_user_state='$user_state' WHERE gu_user_id=" . $userid . " AND gu_group_id=" . $this->id;
			$db->query($sql);
			$this->users = $db->get_var("SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_user_state='approved' AND gu_group_id=$this->id");
			$this->store();
			return true;	
		}
		$db->query("INSERT INTO " . table_group_user . " (gu_group_id,gu_user_id,gu_user_type,gu_user_state) VALUES ($this->id,$userid,'$user_type','$user_state')");
		$this->users = $db->get_var("SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_user_state='approved' AND gu_group_id=$this->id");
		$this->store();
		return true;		
	}

	function get_user_state($userid) {
		global $db;
		$alreadyuser = $db->get_var("SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_group_id=" . $this->id . " AND gu_user_id=" . $userid);
		if ($alreadyuser > 0) {
			$state = $db->get_var("SELECT gu_user_state FROM " . table_group_user . " WHERE gu_group_id=" . $this->id . " AND gu_user_id=" . $userid);
			return $state;
		}
		else {
			return "notin";
		}
	}

	function get_user_type ($userid) {
		global $db, $current_user;
		$user_type = $db->get_var("SELECT gu_user_type FROM " . table_group_user . " WHERE gu_group_id=" . $this->id . " AND gu_user_id=" . $userid);
		return $user_type;
	}

	function set_user_indexshow($userid, $show) {
		global $db;
		/*if ($show > 0) {
			//check if already eight user show
			$num = $db->get_var("SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_group_id=$this->id AND gu_index_show > 0");
			if ($num > 7) {
				//check if the user already set index show
				$num = $db->get_var("SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_group_id=$this->id AND gu_index_show > 0 AND gu_user_id=$userid");
				if ($num ==0) {
					return false;
				}
				else {
					return true;	
				}
			}
		}*/ //all these check are done in before call this function in the page
		//$sql = "UPDATE " . table_group_user . " SET gu_index_show=$show WHERE gu_group_id=$this->id AND gu_user_id=$userid";
		$db->query("UPDATE " . table_group_user . " SET gu_index_show=$show WHERE gu_group_id=$this->id AND gu_user_id=$userid");
		//return $sql;//
		return true;
	}
	
	function getpusher() {
		global $current_user, $globals, $the_template, $smarty, $db;
		include_once('./Smarty.class.php');
		$main_smarty = new Smarty;
		$main_smarty->compile_check=false;
		$main_smarty->cache_lifetime = 120;
		$main_smarty->cache_dir = "templates_c/";
		$main_smarty->compile_dir = "templates_c/";
		$main_smarty->template_dir = "templates/";
		$main_smarty->config_dir = "";
		$main_smarty->assign('pligg_language', pligg_language);
		$main_smarty->config_load("/languages/lang_" . pligg_language . ".conf");

		if(phpnum() == 4) {
			$main_smarty->force_compile = true;
		}

		$sqlquery = "SELECT COUNT(*) FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='approved' AND gu_group_id=$this->id AND gu_index_show > 0";
		$num = $db->get_var($sqlquery);
		if ($num == 0) {
			$nopusher = "true";
			$main_smarty->assign("nopusher", $nopusher);
		}
		else {
			$nopusher = "false";
			$main_smarty->assign("nopusher", $nopusher);
			$sqlquery = "SELECT * FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='approved' AND gu_group_id=$this->id AND gu_index_show > 0";
			$searchsql = mysql_query($sqlquery);
			$userlist = array();
					
			while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
			foreach($userlist as $key => $val){
				$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			}
			$main_smarty->assign('pusherlist', $userlist);
			$main_smarty->assign('grp_id', $this->id);
		}
		return $main_smarty->fetch($the_template . '/pusher.tpl');
	}

	function set_recol_indexshow($linkid, $show) {
		global $db;
		$sql = "UPDATE " . table_group_link . " SET gl_index_show=$show WHERE gl_group_id=$this->id AND gl_link_id=$linkid";
		$db->query("UPDATE " . table_group_link . " SET gl_index_show=$show WHERE gl_group_id=$this->id AND gl_link_id=$linkid");
		return $sql;
		//return true;
	}

	function getrecop_indexshow() {
		global $current_user, $globals, $the_template, $smarty, $db;
		include_once('./Smarty.class.php');
		$main_smarty = new Smarty;
		$main_smarty->compile_check=false;
		$main_smarty->cache_lifetime = 120;
		$main_smarty->cache_dir = "templates_c/";
		$main_smarty->compile_dir = "templates_c/";
		$main_smarty->template_dir = "templates/";
		$main_smarty->config_dir = "";
		$main_smarty->assign('pligg_language', pligg_language);
		$main_smarty->config_load("/languages/lang_" . pligg_language . ".conf");

		if(phpnum() == 4) {
			$main_smarty->force_compile = true;
		}

		$sqlquery = "SELECT COUNT(gl_link_id) FROM " . table_group_link . "," . table_links . " WHERE gl_group_id=" . $this->id . " AND gl_index_show > 0 AND gl_link_id=link_id AND link_category=1";
		$num = $db->get_var($sqlquery);
		if ($num == 0) {
			$nopusher = "true";
			$main_smarty->assign("nopusher", $nopusher);
		}
		else {
			$nopusher = "false";
			$main_smarty->assign("nopusher", $nopusher);
			$sqlquery = "SELECT " . table_users . ".*, " . table_links . ".link_id " . " FROM " . table_users . ", " . table_link_owner . ", " . table_group_link . ", " . table_links . " WHERE " . table_users . ".user_id=" . table_link_owner . ".owner_id AND " . table_link_owner . ".link_id=" . table_group_link . ".gl_link_id AND " . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=1" . " AND " . table_group_link . ".gl_group_id=$this->id AND " . table_links . ".link_status != 'discard' AND gl_index_show > 0";
			$searchsql = mysql_query($sqlquery);
			$userlist = array();
					
			while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
			foreach($userlist as $key => $val){
				$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			}
			$main_smarty->assign('pusherlist', $userlist);
			$main_smarty->assign('grp_id', $this->id);
		}
		return $main_smarty->fetch($the_template . '/recop.tpl');
	}

	function getrecol_indexshow() {
		global $current_user, $globals, $the_template, $smarty, $db;
		include_once('./Smarty.class.php');
		$main_smarty = new Smarty;
		$main_smarty->compile_check=false;
		$main_smarty->cache_lifetime = 120;
		$main_smarty->cache_dir = "templates_c/";
		$main_smarty->compile_dir = "templates_c/";
		$main_smarty->template_dir = "templates/";
		$main_smarty->config_dir = "";
		$main_smarty->assign('pligg_language', pligg_language);
		$main_smarty->config_load("/languages/lang_" . pligg_language . ".conf");

		if(phpnum() == 4) {
			$main_smarty->force_compile = true;
		}

		$sqlquery = "SELECT COUNT(gl_link_id) FROM " . table_group_link . "," . table_links . " WHERE gl_group_id=" . $this->id . " AND gl_index_show > 0 AND gl_link_id=link_id AND link_category=2";
		$num = $db->get_var($sqlquery);
		if ($num == 0) {
			$nopusher = "true";
			$main_smarty->assign("nopusher", $nopusher);
		}
		else {
			$nopusher = "false";
			$main_smarty->assign("nopusher", $nopusher);
			$sqlquery = "SELECT " . table_links . ".* FROM " . table_group_link . "," . table_links . " WHERE gl_group_id=" . $this->id . " AND gl_index_show > 0 AND gl_link_id=link_id AND link_category=2";
			$searchsql = mysql_query($sqlquery);
			$userlist = array();
					
			while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
			$main_smarty->assign('pusherlist', $userlist);
			$main_smarty->assign('grp_id', $this->id);
		}
		return $main_smarty->fetch($the_template . '/recol.tpl');
	}

	function getgrpinfo() {
		global $current_user, $globals, $the_template, $smarty, $db;
		include_once('./Smarty.class.php');
		include_once(mnminclude.'link.php');
		include_once(mnminclude.'user.php');
		$main_smarty = new Smarty;
		$main_smarty->compile_check=false;
		$main_smarty->cache_lifetime = 120;
		$main_smarty->cache_dir = "templates_c/";
		$main_smarty->compile_dir = "templates_c/";
		$main_smarty->template_dir = "templates/";
		$main_smarty->config_dir = "";
		$main_smarty->assign('pligg_language', pligg_language);
		$main_smarty->config_load("/languages/lang_" . pligg_language . ".conf");

		if(phpnum() == 4) {
			$main_smarty->force_compile = true;
		}

		$link = new Link();
		$link->id = $this->linkid;
		$link->read();
		
		$owner = $db->get_var("SELECT gu_user_id FROM " . table_group_user . " WHERE gu_user_type='owner' AND gu_group_id=$this->id");
		$user = new User();
		$user->id = $owner;
		$user->read();
		
		$main_smarty->assign('grp_id', $this->id);
		$main_smarty->assign('grp_name', $this->name);
		$main_smarty->assign('grp_desc', $this->desc);
		$main_smarty->assign('grp_tags', $link->tags);
		$main_smarty->assign('grp_privacy',$this->privacy);
		$main_smarty->assign('grp_users',$this->users);
		$main_smarty->assign('grp_owner',$user->id);
		$main_smarty->assign('grp_owner_names',$user->names);
		return $main_smarty->fetch($the_template . '/group_viewgrpinfo.tpl');
	}

	function getpusherview() {
		global $current_user, $globals, $the_template, $smarty, $db;
		include_once('./Smarty.class.php');
		$main_smarty = new Smarty;
		$main_smarty->compile_check=false;
		$main_smarty->cache_lifetime = 120;
		$main_smarty->cache_dir = "templates_c/";
		$main_smarty->compile_dir = "templates_c/";
		$main_smarty->template_dir = "templates/";
		$main_smarty->config_dir = "";
		$main_smarty->assign('pligg_language', pligg_language);
		$main_smarty->config_load("/languages/lang_" . pligg_language . ".conf");

		if(phpnum() == 4) {
			$main_smarty->force_compile = true;
		}

		$sqlquery = "SELECT COUNT(*) FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='approved' AND gu_group_id=$this->id AND gu_index_show > 0";
		$num = $db->get_var($sqlquery);
		if ($num == 0) {
			$nopusher = "true";
			$main_smarty->assign("nopusher", $nopusher);
		}
		else {
			$nopusher = "false";
			$main_smarty->assign("nopusher", $nopusher);
			$sqlquery = "SELECT * FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='approved' AND gu_group_id=$this->id AND gu_index_show > 0";
			$searchsql = mysql_query($sqlquery);
			$userlist = array();
					
			while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
			foreach($userlist as $key => $val){
				$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			}
			$main_smarty->assign('pusherlist', $userlist);
			$main_smarty->assign('grp_id', $this->id);
		}
		return $main_smarty->fetch($the_template . '/pusherview.tpl');
	}
	
	function getrecopview_indexshow() {
		global $current_user, $globals, $the_template, $smarty, $db;
		include_once('./Smarty.class.php');
		$main_smarty = new Smarty;
		$main_smarty->compile_check=false;
		$main_smarty->cache_lifetime = 120;
		$main_smarty->cache_dir = "templates_c/";
		$main_smarty->compile_dir = "templates_c/";
		$main_smarty->template_dir = "templates/";
		$main_smarty->config_dir = "";
		$main_smarty->assign('pligg_language', pligg_language);
		$main_smarty->config_load("/languages/lang_" . pligg_language . ".conf");

		if(phpnum() == 4) {
			$main_smarty->force_compile = true;
		}

		$sqlquery = "SELECT COUNT(gl_link_id) FROM " . table_group_link . "," . table_links . " WHERE gl_group_id=" . $this->id . " AND gl_index_show > 0 AND gl_link_id=link_id AND link_category=1";
		$num = $db->get_var($sqlquery);
		if ($num == 0) {
			$nopusher = "true";
			$main_smarty->assign("nopusher", $nopusher);
		}
		else {
			$nopusher = "false";
			$main_smarty->assign("nopusher", $nopusher);
			$sqlquery = "SELECT " . table_users . ".*, " . table_links . ".link_id " . " FROM " . table_users . ", " . table_link_owner . ", " . table_group_link . ", " . table_links . " WHERE " . table_users . ".user_id=" . table_link_owner . ".owner_id AND " . table_link_owner . ".link_id=" . table_group_link . ".gl_link_id AND " . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=1" . " AND " . table_group_link . ".gl_group_id=$this->id AND " . table_links . ".link_status != 'discard' AND gl_index_show > 0";
			$searchsql = mysql_query($sqlquery);
			$userlist = array();
					
			while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
			foreach($userlist as $key => $val){
				$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			}
			$main_smarty->assign('pusherlist', $userlist);
			$main_smarty->assign('grp_id', $this->id);
		}
		return $main_smarty->fetch($the_template . '/recopview.tpl');
	}

	function getrecolview_indexshow() {
		global $current_user, $globals, $the_template, $smarty, $db;
		include_once('./Smarty.class.php');
		$main_smarty = new Smarty;
		$main_smarty->compile_check=false;
		$main_smarty->cache_lifetime = 120;
		$main_smarty->cache_dir = "templates_c/";
		$main_smarty->compile_dir = "templates_c/";
		$main_smarty->template_dir = "templates/";
		$main_smarty->config_dir = "";
		$main_smarty->assign('pligg_language', pligg_language);
		$main_smarty->config_load("/languages/lang_" . pligg_language . ".conf");

		if(phpnum() == 4) {
			$main_smarty->force_compile = true;
		}

		$sqlquery = "SELECT COUNT(gl_link_id) FROM " . table_group_link . "," . table_links . " WHERE gl_group_id=" . $this->id . " AND gl_index_show > 0 AND gl_link_id=link_id AND link_category=2";
		$num = $db->get_var($sqlquery);
		if ($num == 0) {
			$nopusher = "true";
			$main_smarty->assign("nopusher", $nopusher);
		}
		else {
			$nopusher = "false";
			$main_smarty->assign("nopusher", $nopusher);
			$sqlquery = "SELECT " . table_links . ".* FROM " . table_group_link . "," . table_links . " WHERE gl_group_id=" . $this->id . " AND gl_index_show > 0 AND gl_link_id=link_id AND link_category=2";
			$searchsql = mysql_query($sqlquery);
			$userlist = array();
					
			while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
			$main_smarty->assign('pusherlist', $userlist);
			$main_smarty->assign('grp_id', $this->id);
		}
		return $main_smarty->fetch($the_template . '/recolview.tpl');
	}
	
	function getmygroup() {
		global $current_user, $globals, $the_template, $smarty, $db;
		if (!($current_user->authenticated)) {
			return '';
			die();
		}
		include_once('./Smarty.class.php');
		$main_smarty = new Smarty;
		$main_smarty->compile_check=false;
		$main_smarty->cache_lifetime = 120;
		$main_smarty->cache_dir = "templates_c/";
		$main_smarty->compile_dir = "templates_c/";
		$main_smarty->template_dir = "templates/";
		$main_smarty->config_dir = "";
		$main_smarty->assign('pligg_language', pligg_language);
		$main_smarty->config_load("/languages/lang_" . pligg_language . ".conf");

		if(phpnum() == 4) {
			$main_smarty->force_compile = true;
		}

		$sqlquery = "SELECT COUNT(" . table_group . ".group_id) FROM " . table_group . "," . table_group_user . " WHERE group_stage='normal' AND group_id=gu_group_id AND gu_user_id=$current_user->user_id";
		$num = $db->get_var($sqlquery);
		if ($num == 0) {
			$nopusher = "true";
			$main_smarty->assign("nopusher", $nopusher);
		}
		else {
			$nopusher = "false";
			$main_smarty->assign("nopusher", $nopusher);
			$sqlquery = "SELECT " . table_group . ".* FROM " . table_group . "," . table_group_user . " WHERE group_stage='normal' AND group_id=gu_group_id AND gu_user_id=$current_user->user_id";
			$searchsql = mysql_query($sqlquery);
			$userlist = array();
					
			while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
			foreach($userlist as $key => $val){
				$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			}
			$main_smarty->assign('pusherlist', $userlist);
			$main_smarty->assign('grp_id', $this->id);
		}
		return $main_smarty->fetch($the_template . '/mygroup.tpl');
	}
}

?>
